<template>
    <div>
        <!-- html部分 -->
        <div class="login-wrapper">
            <div class="login-container">
                <div class="login-left">
                    <div class="welcome-title">欢迎登录</div>
                    <div class="welcome-subtitle">admin 管理系统</div>
                    <img :src="loginImage" alt="登录宣传图" />
                    <div class="tips">安全高效，轻松管理</div>
                </div>
                <div class="login-right">

                    <div class="avatar-container">
                        <el-avatar :size="100" :src="form.avatar ? form.avatar : defaultAvatar"></el-avatar>
                    </div>
                    <el-form :model="form" ref="form" label-width="80px" :rules="rules" style="margin-top: 30px;">

                        <el-form-item label="账号" prop="name">
                            <el-input v-model="form.name" placeholder="请输入账号/用户名/邮箱/手机号"
                                @keyup.enter.native="getAvatar()"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password
                                @keyup.enter.native="Login()"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" style="width:250px;" @click="Login()">登录</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="success" @click="Register()" style="width: 250px;">注册</el-button>

                        </el-form-item>
                    </el-form>
                </div>

            </div>

        </div>
    </div>
</template>
<script>

// js部分
export default {

    // eslint-disable-next-line
    name: 'Login',
    data() {
        return {
            loginImage: 'http://localhost:8888/file/bj2',
            defaultAvatar: 'http://localhost:8888/file/avatar',
            form: {
                name: 'admin',
                password: 'admin',
                avatar: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入账号', trigger: 'blur' },

                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },

                ]
            }


        }
    },
    methods: {
        Register() {
            this.$router.push('/Register')
        },
        getAvatar() {
            if (this.form.name != "") {
                console.log(this.form.name)
                this.$axios.get('/user/getAvatar/' + this.form.name).then(res => {

                    console.log(res)
                   
                    if (res.code == 200)
                        this.form.avatar = res.msg
                })
            }
        },
        //函数方式
        Login() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    if (this.form.avatar == "") {
                        this.getAvatar()

                        this.$axios.post('/user/login', this.form).then(res => {
                           
                            if (res.code == 200) {
                                console.log(res.data)
                                this.$store.commit('setUser', res.data.user)
                                localStorage.setItem('token', res.data.token)
                                this.$message.success({
                                    message: '登录成功',
                                    type: 'success',
                                    onClose: () => {
                                        this.$router.push('/')
                                    }
                                }
                                )
                            } else {
                                this.$message({
                                    message: '登录失败',
                                    type: 'error'
                                })

                            }
                        })
                    }
                } else {
                    this.$message({
                        message: '请填写用户名和密码',
                        type: 'warning'
                    })
                }
            })
        },


    },
}
</script>

<style scoped>
.login-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    width: 100%;
    background-color: #f0f2f5;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    background: url('http://localhost:8888/file/bj');
}

.login-container {
    display: flex;
    width: 80%;
    background-color: #f0f2f5;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
}


.login-left {
    flex: 1;
    background: linear-gradient(135deg, #4a90e2, #5c6bc0);
    color: white;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 40px;
    border-radius: 10px 0 0 10px;
}

.login-right {
    flex: 1;
    background: white;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 40px;
    border-radius: 0 10px 10px 0;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.welcome-title {
    font-size: 2.5em;
    font-weight: bold;
    margin-bottom: 10px;
}

.welcome-subtitle {
    font-size: 1.2em;
    opacity: 0.9;
    margin-bottom: 20px;
}

.banner-img {
    width: 200px;
    height: auto;
    margin: 20px 0;
    border-radius: 8px;
}

.tips {
    font-size: 0.9em;
    opacity: 0.8;
    margin-top: 20px;
}

.avatar-container {
    text-align: center;
    margin-bottom: 20px;
}

.el-form {
    width: 100%;
    max-width: 350px;
}
</style>